package org.linlinjava.litemall.db.service;

import org.linlinjava.litemall.db.dao.AgedmallFreightTemplateMapper;
import org.linlinjava.litemall.db.dao.AgedmallOrderFeeExtMapper;
import org.linlinjava.litemall.db.domain.AgedmallFreightTemplate;
import org.linlinjava.litemall.db.domain.AgedmallFreightTemplateExample;
import org.linlinjava.litemall.db.domain.AgedmallOrderFeeExt;
import org.linlinjava.litemall.db.domain.AgedmallOrderFeeExtExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AgedmallFreightTemplateService {

    @Resource
    private AgedmallFreightTemplateMapper agedmallFreightTemplateMapper;
    @Resource
    private AgedmallOrderFeeExtMapper agedmallOrderFeeExtMapper;

    public AgedmallFreightTemplate findById(Long id) {
        return agedmallFreightTemplateMapper.selectByPrimaryKey(id);
    }

    public int addOrderExtFeeData(AgedmallOrderFeeExt feeExt) {
        AgedmallOrderFeeExtExample agedmallOrderFeeExtExample = new AgedmallOrderFeeExtExample();
        agedmallOrderFeeExtExample.createCriteria().andOrderCodeEqualTo(feeExt.getOrderCode()).andItemOrderCodeEqualTo(feeExt.getItemOrderCode())
                .andFeeNameEqualTo(feeExt.getFeeName());
        long count = agedmallOrderFeeExtMapper.countByExample(agedmallOrderFeeExtExample);
        if(count == 0) {
            agedmallOrderFeeExtMapper.insertSelective(feeExt);
        }else {
            agedmallOrderFeeExtMapper.updateByExampleSelective(feeExt, agedmallOrderFeeExtExample);
        }
        return 1;
    }

    public AgedmallOrderFeeExt getOrderExtFeeDataByName(String bigOrderCode, String itemOrderCode, String feeName) {
        AgedmallOrderFeeExtExample agedmallOrderFeeExtExample = new AgedmallOrderFeeExtExample();
        agedmallOrderFeeExtExample.createCriteria().andOrderCodeEqualTo(bigOrderCode).andItemOrderCodeEqualTo(itemOrderCode)
                .andFeeNameEqualTo(feeName);
        AgedmallOrderFeeExt feeExt = agedmallOrderFeeExtMapper.selectOneByExample(agedmallOrderFeeExtExample);
        return feeExt;
    }
}
